package edu.iss.demo.service;

import java.util.List;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import edu.iss.demo.exception.LeaveDetailNotFound;
import edu.iss.demo.model.Employee;
import edu.iss.demo.model.LeaveDetail;
import edu.iss.demo.repository.LeaveDetailsRepository;

@Service
public class LeaveDetailServiceImpl implements LeaveDetailService {

	@Resource
	LeaveDetailsRepository leaveDetailsRepository;
	
	@PersistenceContext
	EntityManager entityManager;

	@Override
	@Transactional
	public LeaveDetail create(LeaveDetail s) {
		LeaveDetail e = s;
		return leaveDetailsRepository.save(e);
	}

	@Override
	@Transactional(rollbackFor = LeaveDetailNotFound.class)
	public LeaveDetail delete(long id) throws LeaveDetailNotFound {
		LeaveDetail deletedLeaveDetail= leaveDetailsRepository.findOne(id);

		if (deletedLeaveDetail== null)
			throw new LeaveDetailNotFound();

		deletedLeaveDetail.setDeleteFlag(true);
		
		leaveDetailsRepository.save(deletedLeaveDetail);
		return deletedLeaveDetail;
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional
	public List<LeaveDetail> findAll() {
		
		return entityManager.createQuery("SELECT l FROM LeaveDetail l WHERE l.deleteFlag=:false").setParameter("false", false).getResultList();
	}

	@Override
	@Transactional(rollbackFor = LeaveDetailNotFound.class)
	public LeaveDetail update(LeaveDetail s) throws LeaveDetailNotFound {
		LeaveDetail updatedLeaveDetail = leaveDetailsRepository.findOne(s.getLeaveDetailId());

		if (updatedLeaveDetail== null)
			throw new LeaveDetailNotFound();

		updatedLeaveDetail.setEmployeeId(s.getEmployeeId());
		updatedLeaveDetail.setApplicationDate(s.getApplicationDate());
		updatedLeaveDetail.setStartDate(s.getStartDate());
		updatedLeaveDetail.setEndDate(s.getEndDate());
		updatedLeaveDetail.setLeaveType(s.getLeaveType());
		updatedLeaveDetail.setStatus(s.getStatus());
		updatedLeaveDetail.setApprovalStatus(s.getApprovalStatus());
		updatedLeaveDetail.setReason(s.getReason());

		leaveDetailsRepository.flush();
		return updatedLeaveDetail;
	}

	@Override
	@Transactional
	public LeaveDetail findById(long id) {
		return (LeaveDetail)entityManager.createQuery("SELECT l FROM  LeaveDetail l WHERE l.leaveDetailId=:id AND l.deleteFlag=:false").setParameter("id", id).setParameter("false", false).getSingleResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<LeaveDetail> findLeavesForEmployee(Employee emp) {
		// TODO Auto-generated method stub
		
		return entityManager.createQuery("SELECT e FROM LeaveDetail e WHERE e.employeeId=:emp").setParameter("emp", emp).getResultList();
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<LeaveDetail> findSubordinateLeaves(Employee emp) {
		// TODO Auto-generated method stub
		
		return entityManager.createQuery("SELECT e FROM LeaveDetail e WHERE e.employeeId.reportingTo=:emp").setParameter("emp", emp).getResultList();
	}
}
